package mirrormonkey.core.member;

import java.lang.reflect.Constructor;

import mirrormonkey.core.annotations.ClientSideConstructor;
import mirrormonkey.core.annotations.NamedClientSideConstructor;
import mirrormonkey.framework.SyncAppState;
import mirrormonkey.framework.entity.StaticEntityDataIR;
import mirrormonkey.framework.member.MemberDataIR;
import mirrormonkey.framework.member.StaticMemberData;
import mirrormonkey.util.annotations.parsing.AnnotationParser;
import mirrormonkey.util.annotations.parsing.ConstructorIR;

public class ConstructorDataIR extends ConstructorIR implements MemberDataIR {

	public ConstructorDataIR(AnnotationParser parser, Constructor<?> member) {
		super(parser, member);
		this.addCollectType(ClientSideConstructor.class);
		this.addCollectType(NamedClientSideConstructor.class);
	}

	public ConstructorDataIR(ConstructorDataIR previous, Constructor<?> member) {
		super(previous, member);
		this.addCollectType(ClientSideConstructor.class);
		this.addCollectType(NamedClientSideConstructor.class);
	}

	@Override
	public int compareTo(MemberDataIR o) {
		if (getSortingOrder() != o.getSortingOrder()) {
			return Integer.valueOf(getSortingOrder()).compareTo(
					Integer.valueOf(o.getSortingOrder()));
		}
		Class<?>[] myParamTypes = collectedConstructor.getParameterTypes();
		Class<?>[] otherParamTypes = ((ConstructorDataIR) o).collectedConstructor
				.getParameterTypes();
		if (myParamTypes.length != otherParamTypes.length) {
			return Integer.valueOf(myParamTypes.length).compareTo(
					otherParamTypes.length);
		}
		int val = 0;
		for (int i = 0; i < myParamTypes.length && val == 0; i++) {
			val = myParamTypes[i].getName().compareTo(
					otherParamTypes[i].getName());
		}
		return val;
	}

	@Override
	public StaticMemberData extractData(int id,
			StaticEntityDataIR staticEntityDataIR, SyncAppState appState,
			MemberDataIR connectedMemberIR) {
		return new ConstructorData(id,
				appState.entityProvider
						.getInterpreters(getParameterAnnotations()),
				this.collectedConstructor,
				getCollectedAnnotation(NamedClientSideConstructor.class));
	}

	@Override
	public int getSortingOrder() {
		return 0;
	}

}
